
// 埋点
const sql = require("./src/sql.js");
const http = require("./src/http.js");

class DataTrack {
  constructor (ctx){
    ctx = ctx || {};
    if (!ctx && !ctx.mfutil.deployment) {
      throw new Error('deployment is need!');
    }
    this.deployment = ctx.mfutil.deployment;
    this.context = ctx;
  }

  /**
   * @description: 埋点API  PU UV埋点
   * @param {object} obj 埋点数据
   * @return {object} 结果
   */
  async puuv(obj){
    // 参数校验
    if(obj.type != "flowSource" || !obj.userid || !obj.origin || !obj.projectCode){
      return {
        success: false,
        data: {},
        errorCode: "501",
        errorMessage: 'type must be flowSource, userid or origin or projectCode must be needed!'
      }
    }

    if(this.deployment == 'clondapi'){
      return await sql.insert(this.context, obj);
    }else{
      return await http.request(this.context, obj);
    }
  }

  /**
   * @description: 埋点API  自定义埋点
   * @param {object} obj 埋点数据
   * @return {object} 结果
   */
  async custom(obj){

    // 参数校验
    if(!obj.customType || !obj.userid || !obj.origin || !obj.projectCode){
      return {
        success: false,
        data: {},
        errorCode: "501",
        errorMessage: 'customType or userid or origin or projectCode must be needed!'
      }
    }

    if(this.deployment == 'clondapi'){
      return await sql.insert(this.context, obj);
    }else{
      return await http.request(this.context, obj);
    }
  }
}


exports.DataTrack = DataTrack;